package treasurer;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.BKPayroll;
import controllers.Logs;

public class Payroll extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	
	private Model dateModel = new Model();
	private Model activityModel = new Model();
	private Model purposeModel = new Model();
	private Model disbursedByModel = new Model();
	private Model approvedByModel = new Model();
	
	
	public Payroll() throws IOException, SQLException
	{
		
		TextField date = new TextField("date", dateModel);
		date.setRequired(true);
		
		TextField activity = new TextField("activity", activityModel);
		activity.setRequired(true);
		
		TextField purpose = new TextField("purpose", purposeModel);
		purpose.setRequired(true);
		
		TextField disbursedBy = new TextField("disbursedBy", disbursedByModel);
		disbursedBy.setRequired(true);
		
		TextField approvedBy = new TextField("approvedBy", approvedByModel);
		approvedBy.setRequired(true);
		
		
		Form form = new Form("PayrollForm");
		
		form.add(new Button("add"){
		
		private static final long serialVersionUID = 1L;

		public void onSubmit(){
			
			try {
				
			String date = (String)dateModel.getObject();
			String activity = (String)activityModel.getObject();
			String purpose = (String)purposeModel.getObject();
			String disbursedBy = (String)disbursedByModel.getObject();
			String approvedBy = (String)approvedByModel.getObject();
			
			String uploader = ((Sessions)getSession()).getDesc();
			
			Date today = new Date();
			Timestamp timestamp = new Timestamp(today.getTime());
			String ts = timestamp.toString();
			
			BKPayroll bkp = new BKPayroll();
			
			bkp.setDate(date);
			bkp.setActivity(activity);	
			bkp.setPurpose(purpose);
			bkp.setDisbursedBy(disbursedBy);
			bkp.setApprovedBy(approvedBy);			
			bkp.setDate_created(ts);
			bkp.setUploader(uploader);
			bkp.setStatus("PENDING");
			
			System.out.println(
			" Date: " + date + 
			" Activity: " + activity + 
			"Purpose: " + purpose + 
			"Disbursed By: " + disbursedBy + 
			"Approved By: " + approvedBy +
			"Date Created: " + ts +
			" Uploader: " + uploader
			);
			
			sqlMap.insert("BKPayroll.insert", bkp);
			
			Logs log = new Logs();
			log.setLog_activity("Payroll: " + activity);
			log.setLog_officer(uploader);
			log.setLog_time(ts);
			
			sqlMap.insert("Logs.insert", log);
			
			PayrollList page = new PayrollList();
			setResponsePage(page);
			error("added");
			
			}
			catch (SQLException e){
				 error("Fill up necessary fields");
				 e.printStackTrace();
			  } catch (IOException e) {
				e.printStackTrace();
			}
		}});
		
				
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("home", TreasurerHome.class));
		add(new BookmarkablePageLink("view", PayrollList.class));
		add(new BookmarkablePageLink("manageForms", ManageForms.class));
		
		
		add(new FeedbackPanel("error"));
		
		form.add(date);
		form.add(activity);
		form.add(purpose);
		form.add(approvedBy);
		form.add(disbursedBy);
		
		add(form);
	}

}